package androidtool.md5key;

import androidtool.MainMenu;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Hoang
 */
public class MD5MainGUI extends javax.swing.JFrame {

    private MainMenu mainMenu;
    private MyKey myKey;

    /**
     * Creates new form Main
     */
    public MD5MainGUI(MainMenu mainMenu) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        }

        this.mainMenu = mainMenu;

        initComponents();
        setLocation(200, 150);
        setVisible(true);

        myKey = new MyKey();
        lblMD5Key.setText(myKey.getMD5());
        lblSHA1Key.setText(myKey.getSHA1());
        lblSHA256Key.setText(myKey.getsHA256());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblMD5Key = new javax.swing.JLabel();
        btnCopyMD5key = new javax.swing.JButton();
        btnCopySHA1Key = new javax.swing.JButton();
        lblSHA1Key = new javax.swing.JLabel();
        btnCopySHA256 = new javax.swing.JButton();
        lblSHA256Key = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("My Key");

        lblMD5Key.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblMD5Key.setBorder(javax.swing.BorderFactory.createTitledBorder("MD5 key"));

        btnCopyMD5key.setText("Copy MD5 key");
        btnCopyMD5key.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCopyMD5keyActionPerformed(evt);
            }
        });

        btnCopySHA1Key.setText("Copy SHA1 key");
        btnCopySHA1Key.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCopySHA1KeyActionPerformed(evt);
            }
        });

        lblSHA1Key.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblSHA1Key.setBorder(javax.swing.BorderFactory.createTitledBorder("SHA1 key"));

        btnCopySHA256.setText("Copy SHA256 key");
        btnCopySHA256.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCopySHA256ActionPerformed(evt);
            }
        });

        lblSHA256Key.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblSHA256Key.setBorder(javax.swing.BorderFactory.createTitledBorder("SHA256 key"));

        jButton1.setText("Get Google Map key");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Open Main menu");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCopySHA256, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCopySHA1Key, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCopyMD5key, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMD5Key, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblSHA1Key, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addComponent(lblSHA256Key, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblMD5Key, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addComponent(btnCopyMD5key, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblSHA1Key, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addComponent(btnCopySHA1Key, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnCopySHA256, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addComponent(lblSHA256Key, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCopyMD5keyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCopyMD5keyActionPerformed
        StringSelection sers = new StringSelection(lblMD5Key.getText());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sers, null);
    }//GEN-LAST:event_btnCopyMD5keyActionPerformed

    private void btnCopySHA1KeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCopySHA1KeyActionPerformed
        StringSelection sers = new StringSelection(lblSHA1Key.getText());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sers, null);
    }//GEN-LAST:event_btnCopySHA1KeyActionPerformed

    private void btnCopySHA256ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCopySHA256ActionPerformed
        StringSelection sers = new StringSelection(lblSHA256Key.getText());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sers, null);
    }//GEN-LAST:event_btnCopySHA256ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String link = "http://www.google.com/glm/mmap/a/api?fp=";
        String md5 = myKey.getMD5();
        String[] arrMD5 = md5.split(":");
        int size = arrMD5.length;
        for (int i = 0; i < size; i++) {
            link += arrMD5[i];
            if (i < size - 1) {
                link += "%3A";
            }
        }
        System.out.println("link: " + link);
        try {
            Desktop.getDesktop().browse(java.net.URI.create(link));
        } catch (IOException ex) {
            Logger.getLogger(MD5MainGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        if (mainMenu == null) {
            mainMenu = new MainMenu();
        }
        mainMenu.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCopyMD5key;
    private javax.swing.JButton btnCopySHA1Key;
    private javax.swing.JButton btnCopySHA256;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JLabel lblMD5Key;
    private javax.swing.JLabel lblSHA1Key;
    private javax.swing.JLabel lblSHA256Key;
    // End of variables declaration//GEN-END:variables
}
